#include "Config.h"

using namespace Ogre;

Config Config::mConfig;
Ogre::Real Config::CHATBOX_LEFTPOS = 0.0f;
Ogre::Real Config::CHATBOX_TOPPOS = 0.0f;
Ogre::Real Config::CHATBOX_WIDTH = 0.0f;
Ogre::Real Config::CHATBOX_HEIGHT = 0.0f;

Config::Config() :
	cfg_soundOn(false),
	cfg_debugOn(true)
{
}

void Config::saveConfig()
{
	/*TiXmlDocument doc;
 	TiXmlDeclaration* decl = new TiXmlDeclaration( "1.0", "", "" );  
	doc.LinkEndChild( decl );  
 
	// Set Plitzkrieg as main tag i.e. root
	TiXmlElement * root = new TiXmlElement( "Plitzkrieg" );  
	doc.LinkEndChild( root );  
 
	// Add 'Config' Section to Plitzkrieg
	TiXmlElement * config = new TiXmlElement( "Config" );
	root->LinkEndChild( config );

	// Add 'cfg_soundOn' under Config
	TiXmlElement * soundOn = new TiXmlElement( "cfg_soundOn" );
	soundOn->SetAttribute("value", cfg_soundOn);
	config->LinkEndChild( soundOn );	

	doc.SaveFile( "config.xml" );*/
}

void Config::loadConfig(const Ogre::String &filename, const Ogre::String &groupName)
{
	TiXmlDocument   *XMLDoc = 0;
	TiXmlElement   *XMLRoot;

	try
	{
		// Strip the path
		Ogre::String basename, path;
		Ogre::StringUtil::splitFilename(filename, basename, path);

		DataStreamPtr pStream = ResourceGroupManager::getSingleton().openResource( basename, groupName );
		String data = pStream->getAsString();

		// Open the .xml File
		XMLDoc = new TiXmlDocument();
		XMLDoc->Parse( data.c_str() );
		pStream->close();
		pStream.setNull();

		if( XMLDoc->Error() )
		{
			//We'll just log, and continue on gracefully
			LogManager::getSingleton().logMessage("[Config] The TiXmlDocument reported an error");
			delete XMLDoc;
			return;
		}
	}
	catch(...)
	{
		//We'll just log, and continue on gracefully
		LogManager::getSingleton().logMessage("[Config] Error creating TiXmlDocument");
		delete XMLDoc;
		return;
	}

	// Validate the File
	XMLRoot = XMLDoc->RootElement();
	if( String( XMLRoot->Value()) != "Plitzkrieg"  ) {
		LogManager::getSingleton().logMessage( "[Config] Error: Invalid File." );
		delete XMLDoc;      
		return;
	}

	loadXMLFile(XMLRoot);
}

void Config::loadXMLFile(TiXmlElement* XMLRoot)
{
	// Processes all the sub tags in Plitkrieg
	TiXmlElement* cfg = XMLRoot->FirstChildElement("ChatBox");
	if (cfg) processChatBox(cfg);
}

void Config::processChatBox(TiXmlElement* cfg)
{
	TiXmlElement* pElement;
	pElement = cfg->FirstChildElement("CHATBOX_LEFTPOS");
	if (pElement)
		Config::CHATBOX_LEFTPOS = StringConverter::parseReal(pElement->Attribute("value"));
	
	pElement = cfg->FirstChildElement("CHATBOX_TOPPOS");
	if (pElement)
		Config::CHATBOX_TOPPOS = StringConverter::parseReal(pElement->Attribute("value"));

	pElement = cfg->FirstChildElement("CHATBOX_WIDTH");
	if (pElement)
		Config::CHATBOX_WIDTH = StringConverter::parseReal(pElement->Attribute("value"));

	pElement = cfg->FirstChildElement("CHATBOX_HEIGHT");
	if (pElement)
		Config::CHATBOX_HEIGHT = StringConverter::parseReal(pElement->Attribute("value"));
}